// pages/rank/index.js
const $http = require('../../request/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    first_prize: {},
    second_prize: {},
    third_prize: {},
    avatarUrl: "",
    totalScore: 0,
    rank: 0,
    my_name: "",
    others_competitor: [
    ]
  },
  onLoad: function (options) {
    let that = this;
    this.getAllRank().then(data => {
      that.getmyRank(data)
    })
    this.getmyRank()
  },
  onShow: function () {
    this.getAllRank()
    this.getmyRank()

  },
  // 获取所有用户的排名
  getAllRank() {
    return new Promise((resovle, reject) => {
      $http.request("GET", `/front/user/ranks`, "").then(res => {
        let { data } = res.data;
        console.log("data", data)
        let tmp = [];
        for (let i = 3; i < data.length; i++) {
          tmp.push(data[i])
        }
        this.setData({
          first_prize: data[0],
          second_prize: data[1],
          third_prize: data[2],
          others_competitor: tmp,
        })
        resovle(data)

      })
    })

  },

  getmyRank(data) {
    // data 是所有排名
    $http.request("GET", `/front/user/rank`, "").then(res => {
      let app = getApp();
      // dataMessage 是索引
      let dataMessage = res.data.data
      if (dataMessage != null) {
        console.log("data", data)
        this.setData({
          totalScore: app.userInfo.totalScore,
          my_name: app.userInfo.name,
          avatarUrl: app.userInfo.avatarUrl,
          rank: dataMessage,
        })
      } else {
        this.setData({
          my_name: app.userInfo.name,
          avatarUrl: app.userInfo.avatarUrl,
          totalScore: app.userInfo.totalScore,
          rank: dataMessage,
        })
      }
    })
  }
})